1.             For each of the following compiler driver command lines, indicate the sequence of programs that will be executed.  When you list the programs, also indicate which of the command line options will be passed to that program. Explain each answer.  The first one is done for you to show you what I mean.

A.      g++ -g –Wall –lm –Ddebug –I/usr/local/pgsql/include –o test test.o

Answer: ld –lm test.o –o test

Explanation: Only the linker would be run, and the –lm and –o options would be passed to it by the compiler driver.

B.       g++ -g –Wall –lm –Ddebug –I/usr/local/pgsql/include –o test test.cc exam.cc

C.       g++ -g –Wall –lm –Ddebug –I/usr/local/pgsql/include –o test test.cc exam.o

D.      g++ -g –Wall –lm –Ddebug –I/usr/local/pgsql/include –o test test.o exam.o

  1. g++ -g –Wall –lm –Ddebug –I/usr/local/pgsql/include –c test.cc

2.             Answer the following questions about RCS.  Assume the current directory already has a subdirectory named RCS.

A.      What command would be used to put a file named test.cc under RCS control, and what would happen when this command is executed?  Tell what the user would see happening as the command is run, and tell what files would be created, deleted, or changed by this command.

B.       After checking in 6 versions of a file named test.cc, what file(s) related to test.cc would exist in the RCS subdirectory?

C.       What are the permissions for the files in the RCS subdirectory?

D.      How many people can lock a file for editing at the same time?

E.       How do you lock a file for editing?

F.       How many people can get copies of a file that are not locked for editing at the same time?

G.       How do you get a copy of a file that is not locked for editing?

H.      If there are six versions of test.cc numbered 1.1 through 1.6, how can you get a copy of version 1.3 that is not locked for editing?

I.         If there are six versions of test.cc numbered 1.1 through 1.6, how can you get a copy of version 1.3 that is locked for editing?

J.        I have checked out version 1.6 of test.cc for editing and made some changes.  Now I want to save it as version 2.1.  What command would I use to do this?

K.      What will happen when I enter the command for question J?  Tell what the user would see happening as the command is run, and tell what files would be created, deleted, or changed by this command.

3.             Write a complete Makefile with rules for depend, clean, and cmd.  The source files for cmd are cmd.cc and utils.cc, both of which #include a header file named utils.h.  The default rule (if no arguments are passed on the command line) should be cmd.  Design the Makefile so other targets and source files can be added to the project easily.

4.             Here is a prototype for a function:

int dispatch( char* whichOne, int value );

 Description: Function dispatch() uses a global array named dispatchTable to call another function which is associated with the string whichOne in the same way that we associated builtin functions with command names in the qsh project.  But for this question, the functions listed in the dispatch table all take one int as an argument and return another int as a result.  Function dispatch() passes the value of value as the argument to the function it calls.

A.      Write a suitable prototype for the functions to be listed in the dispatch table.

B.       Write the code for dispatchTable, including an appropriate struct to hold each element of the array.  Initialize dispatchTable with entries for two functions, doMethodA(), which is associated with the string “methoda,” and doMethodB(), which is associated with the string “methodb.”  Include initialization of a constant int named numFuncs, which will have the length of the dispatchTable array, no matter how many entries are in the array.

C.       Write the code to define function dispatch().  If there is no entry in dispatchTable matching the value of whichOne, dispatch() is to return –1 (negative one); otherwise it is to return the value returned by the function associated with whichOne.

5.             Give short answers to the following questions.

  1. What is wrong with this code?
    char* ptr; *ptr = ‘x’;
  2. What is wrong with this code?
    char buf[256]; scanf( “%s”, buf ); if ( buf == “quit” ) exit( 0 );
  3. What is the difference between return 0; and exit( 0 );?  (I know, one says return and the other says exit.  Try to say something a bit more profound than that!)
  4. What would the following code print?  Assume ints are 4 bytes and pointers are 8 bytes.
  5. int* a = (int*)malloc( 4*sizeof(int));
    int  b[ ] = { 4, 3, 2, 1 };
    printf(“%d %d\n”, sizeof(a), sizeof(b));
  6. What value does the fork() system call return?

“PATH=/usr/bin:/usr/local/bin”

 
6.             When main() is called, it is passed three arguments, traditionally named argc, argv,and envp.  The values of these three arguments are passed to main() on the stack.  Assume a user enters the command line, “cmd hello there” and that the user’s environment has just two variables set, PATH=/usr/bin:/usr/local/bin and HOME=/usr/users/auser.  Assume an int is the same size as a pointer on the user’s computer.  Draw a diagram that shows argc, argv, envp,and all the data pointed to by them when main() is called for this command.  The diagram is started for you below, but draw your own complete diagram in your answer book.